<?php

!defined('ROOT_PATH') && exit('Forbidden');

/**
 *    支付方式基础类
 *
 *    @author    Garbin
 *    @usage    none
 */
class BasePayment extends Object
{
    /* 外部处理网关 */
    var $_gateway   = '';
    /* 支付方式唯一标识 */
    var $_code      = '';


    function __construct($payment_info = array())
    {
        $this->BasePayment($payment_info);
    }
    function BasePayment($payment_info = array())
    {
        $this->_info   = $payment_info;
        $this->_config = unserialize($payment_info['config']);
    }

    /**
     *    获取支付表单
     *
     *    @author    Garbin
     *    @param     array $order_info
     *    @return    array
     */
    function get_payform()
    {
        return $this->_create_payform('POST');
    }
    
    /**
     *    获取规范的支付表单数据
     *
     *    @author    Garbin
     *    @param     string $method
     *    @param     array  $params
     *    @return    void
     */
    function _create_payform($method = '', $params = array())
    {
        return array(
            'online'    =>  $this->_info['is_online'],
            'desc'      =>  $this->_info['payment_desc'],
            'method'    =>  $method,
            'gateway'   =>  $this->_gateway,
            'params'    =>  $params,
        );
    }

    /**
     *    获取通知地址
     *
     *    @author    Garbin
     *    @param     int $store_id
     *    @param     int $order_id
     *    @return    string
     */
    function _create_notify_url($order_id)
    {
        return SITE_URL . "/index.php?app=paynotify&act=notify&order_id={$order_id}";
    }

    /**
     *    获取返回地址
     *
     *    @author    Garbin
     *    @param     int $store_id
     *    @param     int $order_id
     *    @return    string
     */
    function _create_return_url($order_id)
    {
        return SITE_URL . "/index.php?app=paynotify&order_id={$order_id}";
    }

    /**
     *    获取外部交易号
     *
     *    @author    Garbin
     *    @param     array $order_info
     *    @return    string
     */
    function _get_trade_sn($order_info)
    {
        $out_trade_sn = $order_info['out_trade_sn'];
        if (!$out_trade_sn)
        {
            $out_trade_sn = $this->_config['pcode'] . $order_info['order_sn'];

            /* 将此数据写入订单中 */
            $model_order =& m('order');
            $model_order->edit(intval($order_info['order_id']), array('out_trade_sn' => $out_trade_sn));
        }

        return $out_trade_sn;
    }

    /**
     *    获取商品简介
     *
     *    @author    Garbin
     *    @param     array $order_info
     *    @return    string
     */
    function _get_subject($order_info)
    {
        return '正在支付'; 
    }

    /**
     *    获取通知信息
     *
     *    @author    Garbin
     *    @return    array
     */
    function _get_notify()
    {
        /* 如果有POST的数据，则认为POST的数据是通知内容 */
        if (!empty($_POST))
        {
            return $_POST;
        }

        /* 否则就认为是GET的 */
        return $_GET;
    }

    /**
     *    验证支付结果
     *
     *    @author    Garbin
     *    @return    void
     */
    function verify_notify()
    {
        #TODO
    }

    /**
     *    将验证结果反馈给网关
     *
     *    @author    Garbin
     *    @param     bool   $result
     *    @return    void
     */
    function verify_result($result)
    {
        if ($result)
        {
            echo 'success';
        }
        else
        {
            echo 'fail';
        }
    }
    function get_order_info($order_info){
        if($order_info['is_order_array']){
            $order_info0=$order_info;
            $order_info=array();
            $order_type = & ot('normal');
            foreach($order_info0['item'] as $fe_order_info){
                if(!$order_info['money']){
                    $order_info['money']=bcsub($fe_order_info['order_amount'] , $fe_order_info['pd_amount'],2);
                }else{
                    $order_info['money']=bcadd($order_info['money'],bcsub($fe_order_info['order_amount'] , $fe_order_info['pd_amount'],2),2);
                }
                
                if(!($new_order_sn)){
                    $new_order_sn=$order_type->_gen_order_sn();
                    $out_trade_no=$new_order_sn;
                    $orders_group=$fe_order_info['order_id'];
                    $order_info['buyer_id']=$fe_order_info['buyer_id'];
                    $order_info['payment_name']=$fe_order_info['payment_name'];
                    $order_info['buyer_name']=$fe_order_info['buyer_name'];
                    $order_info['order_sn']=$new_order_sn;
                    $order_info['order_id']=$fe_order_info['order_id'];
                }else{
                    $orders_group.=','.$fe_order_info['order_id'];
                }
                

            }
                $account_log_mod = &m('account_log');
                $account_log = array(
                    'user_id' => $order_info['buyer_id'],
                    'user_name' => $order_info['buyer_name'],
                    'order_sn' => $new_order_sn,
                    'orders_group' => $orders_group,
                    'add_time' => gmtime(),
                    'type' => ACCOUNT_TYPE_CZ,
                    'money_flow' => 'income',
                    'money' => $order_info['money'],
                    'log_text' => $order_info['buyer_name'] . "充值" . $order_info['money'] . "元(".$order_info['payment_name'].")",
                );
                $account_log_mod->add($account_log);
        }else{
        $order_info['money'] = bcsub($order_info['order_amount'] , $order_info['pd_amount'],2);
        $order_info = $this->add_account_log($order_info);
        if(!$order_info){
            return false;
        }
        }
        return $order_info;
    }
    
    function add_account_log($order_info) {
        $account_log_mod = &m('account_log');
        $account_log = $account_log_mod->get('order_sn=' . $order_info['order_sn']);
        if (empty($account_log)) {
            $account_log = array(
                'user_id' => $order_info['buyer_id'],
				'order_id' => $order_info['order_id'],
                'user_name' => $order_info['buyer_name'],
                'order_sn' => $order_info['order_sn'],
                'add_time' => gmtime(),
                'type' => ACCOUNT_TYPE_CZ,
                'money_flow' => 'income',
                'money' => $order_info['money'],
                'log_text' => $order_info['buyer_name'] . "充值" . $order_info['money'] . "元(".$order_info['payment_name'].")",
            );
            $account_log_mod->add($account_log);
        }else{
            if($account_log['complete']==1){
                return false;
            }
            //如果更换支付方式，记录需发生变化。
            $data['log_text'] = $order_info['buyer_name'] . "充值" . $order_info['money'] . "元(".$order_info['payment_name'].")";
            if($account_log['money']!=$order_info['money']){
                //订单金额发生改变会导致不允许支付，需要更换订单号
                $order_type = & ot('normal');
                $new_order_sn=$order_type->_gen_order_sn();
                //金额不一致需修改。
                $data['money'] = $order_info['money'];
                $data['order_sn'] = $new_order_sn;
                //修改订单order_sn
                $order_mod=& m('order');
                $order_mod->edit($order_info['order_id'],'order_sn='.$new_order_sn);
                $order_info['order_sn']=$new_order_sn;
            }
            $account_log_mod->edit($account_log['id'],$data);
        }
        return $order_info;
    }
    
}

?>